import Vue from "vue";
import dateFormat from 'dateformat';
import axios from "../framework/network/request";
import validator from "../framework/validator/index";
import ElementUI from "element-ui";
import cookies from 'js-cookie';
import queryString from 'query-string';

export default class App {
  constructor(config) {
    this.config = config;
    Vue.use(ElementUI);
  }

  bootstrap() {
    const options = this.create(window.__INITIAL_STATE__);

    if (!Vue.prototype.hasOwnProperty("$axios")) {
      Vue.prototype["$axios"] = axios;
    }

    if (!Vue.prototype.hasOwnProperty("$cookies")) {
      Vue.prototype["$cookies"] = cookies;
    }

    if (!Vue.prototype.hasOwnProperty("$dateformat")) {
      Vue.prototype["$dateformat"] = dateFormat;
    }

    if (!Vue.prototype.hasOwnProperty("$validator")) {
      Vue.prototype["$validator"] = validator;
    }

    if (!Vue.prototype.hasOwnProperty("$query")) {
      Vue.prototype["$query"] = queryString;
    }

    const app = new Vue(options);
    app.$mount("#app");
  }

  create() {
    const {
      index,
      options,
      createRouter,
    } = this.config;
    
    let condition = {
      ...index,
      ...options,
    }

    if (createRouter) {
      const router = createRouter();
      condition.router = router;
    }
    return condition;
  }
}
